const {specialty} = require('../dbConfig/index.js')
const { Op } = require('sequelize')

const addSpecialty = async (ctx,next)=>{
	const {  specialty_name,specialty_Date,specialty_score,specialty_school_id } = ctx.request.body
	if(  !specialty_name||!specialty_Date||!specialty_score||!specialty_school_id){
		ctx.body = {
			code:1,
			data:null,
			msg:'请填写完整信息'
		}
		return
	}
	const res = await specialty.create({
		specialty_name,
		specialty_Date,
		specialty_score,
		specialty_school_id
	})
	ctx.body = {code:200,msg:'成功',data:null}
}

const getSpecialty = async (ctx,next)=>{
	// console.log('获取预约列表',ctx.query)
	const {school_id} = ctx.query
	if(school_id){
		const res = await specialty.findAll({
			where:{
				specialty_school_id:school_id
			}
		})
		ctx.body = {
			code:200,
			data:res,
			msg:'获取成功'
		}
	}else{
		ctx.body = {
			code:1,
			data:null,
			msg:'缺少院校ID'
		}
	}
}
// 修改专业信息  TODO  会修改该专业的所有学生信息  需要确认是否可以修改
const editSpecialty = async (ctx,next)=>{
	const { specialty_id,specialty_name,specialty_Date,specialty_score,specialty_school_id } = ctx.request.body
	if(  !specialty_id||!specialty_name||!specialty_Date||!specialty_score){
		ctx.body = {
			code:1,
			data:null,
			msg:'请填写完整信息'
		}
		return
	}
	 await specialty.update({
		specialty_name,
		specialty_Date,
		specialty_score
	},{
		where:{
			specialty_id
		}
	})
	ctx.body = {code:200,msg:'成功',data:null}
}

// 删除专业  TODO  检查是否还有学生 有则不能删除
const deleteSpecialty = async (ctx,next)=>{
	const { specialty_id } = ctx.request.body
	if(  !specialty_id){
		ctx.body = {
			code:1,
			data:null,
			msg:'请填写完整信息'
		}
		return
	}
	await specialty.destroy({
		where:{
			specialty_id
		}
	})
	ctx.body = {code:200,msg:'成功',data:null}
}
module.exports = {
	addSpecialty,
	getSpecialty,
	editSpecialty,
	deleteSpecialty
}